package edu.bbu.commonproject.desktop.gui.view.swing.file.lister.utils;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import edu.bbu.commonproject.desktop.gui.view.swing.utils.Design;
import edu.bbu.commonproject.desktop.gui.view.swing.utils.TexturedPanel;

/**
 * The directory creator.
 */
public class DirectoryCreator extends JDialog
{
	/** It initializes the CREATE_ANSWER. */
	public static final int CREATE_ANSWER = 0;
	
	/** It initializes the CANCEL_ANSWER. */
	public static final int CANCEL_ANSWER = 1;
	
	/** The create button. */
	private JButton createButton;
	
	/** The cancel button. */
	private JButton cancelButton;
	
	/** The label's name. */
	private JLabel nameLabel;
	
	/** The text field's name. */
	private JTextField nameTextField;
	
	/** The back panel. */
	private TexturedPanel backPanel;
	
	/** It initializes the answer. */
	private int answer = CANCEL_ANSWER;
	
	/**
	 * The constructor.
	 * @param parrentFrame the parrent frame.
	 */
	public DirectoryCreator(JFrame parrentFrame)
	{
		super(parrentFrame, true);
		
		this.setResizable(false);
		this.setTitle("New directory");
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		setComponents();
		
		this.setLocationRelativeTo(parrentFrame);
	}
	
	/**
	 * It sets the components.
	 */
	private void setComponents()
	{
		backPanel = new TexturedPanel(Design.DIALOG_NEW_DIRECTORY);
		backPanel.setPreferredSize(new Dimension(400, 125));
		backPanel.setLayout(null);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setLocation(275, 88);
		cancelButton.setSize(new Dimension(120, 30));
		cancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				answer = CANCEL_ANSWER;
				close();
			}
		});
		
		createButton = new JButton("Create");
		createButton.setLocation(145, 88);
		createButton.setSize(new Dimension(120, 30));
		createButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				answer = CREATE_ANSWER;
				close();
				
			}
		});
		
		nameLabel = new JLabel("Directory name");
		nameLabel.setLocation(100, 10);
		nameLabel.setSize(new Dimension(200, 25));
		
		nameTextField = new JTextField();
		nameTextField.setLocation(100, 40);
		nameTextField.setSize(new Dimension(280, 25));
		nameTextField.setBorder(BorderFactory.createEtchedBorder());
		nameTextField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				answer = CREATE_ANSWER;
				close();
			}
		});

		backPanel.add(nameLabel);
		backPanel.add(cancelButton);
		backPanel.add(createButton);
		backPanel.add(nameTextField);
		
		this.add(backPanel, BorderLayout.CENTER);
		this.pack();
		
		nameTextField.requestFocus();
	}

	/**
	 * It closes the frame.
	 */
	private void close()
	{
		this.setVisible(false);
		this.dispose();
	}
	
	/**
	 * It shows the dialog.
	 */
	public void showDialog()
	{
		this.setVisible(true);
	}
	
	/**
	 * It gets the answer.
	 * @return the answer.
	 */
	public int getAnswer()
	{
		return answer;
	}
	
	/**
	 * It gets the directory name.
	 * @return the directory name.
	 */
	public String getDirectoryName()
	{
		return nameTextField.getText();
	}
}
